<input type="hidden" id="txtcodcontriant"/>

<div id="panelRegistro">
    <div align="center" class="ui-widget ui-widget-content ui-corner-all" style="width: 99%;margin: 3px;">
        <div class="ui-widget ui-state-default ui-corner-top ui-title">
            <label>Busqueda Contribuyente</label>
        </div>
        <table class="ui-panelLayout-main" border="0" cellpadding="0" cellspacing="0">
            <tr><td style="vertical-align: top;">
                    <table style="width: 100%;" border="0">
                        <tr>
                            <td width="20">&nbsp;</td>
                            <td width="100">C&oacute;digo:</td>
                            <td width="10">&nbsp;</td>
                            <td width="150">Nombre:</td>
                            <td width="10">&nbsp;</td>
                            <td width="150">Apellido Paterno:</td>
                            <td width="10">&nbsp;</td>
                            <td width="150" colspan="5">Apellido Materno:</td>
                            <td width="20">&nbsp;</td>
                            <td rowspan="2">
                                <button id="btnbuscarcontri" >Buscar</button>
                                <!-- <button id="btncontri" name="btncontri">Contribuyente</button> -->
                                <!-- <button id="btnestcta" name="btnestcta">Estado de Cuenta</button> -->
                            </td>
                        </tr>
                        <tr id="panelContribuyenteBusq">
                            <td width="20">&nbsp;</td>
                            <td width="100"><input class="ui-text" id="c_codigocontrib" maxlength="10" style="width:90px" /></td>
                            <td width="10">&nbsp;</td>
                            <td width="150"><input class="ui-text pnl" id="c_nombrecontrib" style="width:100%" /></td>
                            <td width="10">&nbsp;</td>
                            <td width="150"><input class="ui-text pnl" id="c_apepatcontrib" style="width:100%" /></td>
                            <td width="10">&nbsp;</td>
                            <td width="150" colspan="5"><input class="ui-text pnl" id="c_apematcontrib" style="width:100%" /></td>
                            <td width="20">&nbsp;</td>
                        </tr>
                        <tr>
                            <td colspan="13" height="8px"></td>
                        </tr>                      
                        <tr>
                            <td colspan="14" align="center">
                                <br/>
                                <div id="panelResult">
                                    <table id="tblResult"></table>
                                    <div id="ptblResult"></div>
                                    <input type="hidden" id="ctblResult" name="ctblResult" value="" />
                                </div>
                            </td>
                        </tr>
                    </table>
                </td></tr>
        </table>
    </div>
</div>


<script type="text/javascript"> 
idTimeOut = 0; 

verDetalle = function(rowid) {
	row = $("#tblResult").jqGrid('getRowData', rowid);

	console.log(row);    

	$("#txtcodcontri").val(row.cidpers);
	$("#txtcontrinomcond").val(row.crazsoc);
	
	closeDialog("jqDialog3");
};

gridCompleteTblResult = function(){
	var ids = $("#tblResult").jqGrid('getDataIDs');
	for(var i=0; i < ids.length; i++){ 
		id = ids[i]; 
		bt = "<a style='height:20px;width:20px;' class='btnGrid' href='javascript: void(0);' onclick=\"verDetalle('"+id+"');\"><img src='" + pathImage + "find.png'/></a>";
		$("#tblResult").jqGrid('setRowData', ids[i], {modedit: bt});
	}
};

optionContribuyente = {
    width: 650,
    colNames: ["C\u00F3digo", "Administrado", "Direcci\u00F3n Fiscal"],
    colModel: [
        {name:'cidpers', index:'cidpers', width:80, align: 'center'},
        {name:'crazsoc', index:'crazsoc', width:420},
        {name:'direccf', index:'direccf', width:420} ],
    caption: "&nbsp;&nbsp;&nbsp;Resultados de la busqueda",
    onSelectRow: verDetalle

};
bindkeysContribuyente = {"onEnter": verContribuyentePredio};


buscarContribuyente = function() {    
    parameters = {
        "name": "tblResult",
        "procedure": "public.buscar_persona",
        "print": "true",
        "parameters": '{' +
        '"p_nvar1":"' + $("#c_codigocontrib").val().toUpperCase() + '",' +
        '"p_nvar2":"' + $("#c_nombrecontrib").val().toUpperCase() + '",' +
        '"p_nvar3":"' + $("#c_apepatcontrib").val().toUpperCase() + '",' +
        '"p_nvar4":"' + $("#c_apematcontrib").val().toUpperCase() + '",' +
        '"p_nvar5":""' +
        '}'
    };

    proceso = function(requestData){
    	$("#panelResult").html('<table id="tblResult"></table><div id="ptblResult"></div><input type="hidden" id="ctblResult" name="ctblResult" value="" />');
    	records = requestData.length;
    	options = $.extend(optionContribuyente, {
			data: requestData,
	        datatype: "local"
    	});
        if(records == 0) {
            inicializarGrid("tblResult", options);
            buttons = {
                "Aceptar": function(){
                    closeDialog("jqDialogInfo");
                    $('#c_codigocontrib').focus();
                }
            };
            openDialogInfo("No existen contribuyentes con los criterios de busqueda ingresados.", 380, 150, buttons);
        } else if(records > 0) {
        	inicializarGrid("tblResult", options, bindkeysContribuyente);
        }
    };

    procesarConsultaSubProceso('registrar', parameters, proceso, 'json');
};


$(function(){
    $("#c_codigocontrib").numeric({
        decimal: false, 
        negative: false
    }, function() {
        openDialogWarning("Solo Numeros Enteros Positivos.", 150, 90);
        this.value = "";
        this.focus();
    });
    

   
    inicializarGrid("tblResult", optionContribuyente);
    
    $("#panelContribuyenteBusq").on("keyup", "input", function(e) {
        if(e.keyCode == 13){
            $("#btnbuscarcontri").click();
        }
    });
        
    $("#c_codigocontrib").on("focus", function() {
        $("#panelContribuyenteBusq input.ui-text").val("");
    });
    
    $(".pnl").on("focus", function() {
        $("#c_codigocontrib").val("");
        $("#c_predial").val("");
    });


    $("#btnbuscarcontri").button({
        icons: {primary:'ui-icon-search'}
    }).bind("click", function(){
        
        valid = [false, false];

        
    
        if(trim($('#c_codigocontrib').val()).length > 0){
            $('#c_codigocontrib').val(LPad($('#c_codigocontrib').val(), 10, '0'));
        }

        valid[0] = valid[0] || (trim($("#c_nombrecontrib").val()).length > 0);
        valid[0] = valid[0] || (trim($("#c_apepatcontrib").val()).length > 0);
        valid[0] = valid[0] || (trim($("#c_apematcontrib").val()).length > 0);
        valid[0] = valid[0] || (trim($("#c_codigocontrib").val()).length > 0);

      	 console.log('Array');
    	 console.log(batch);

		 console.log(batch.length); 	

         for (var i = 0; i < batch.length; i++) {
        	 console.log(batch[i]["mperson"]);

             if ($('#c_codigocontrib').val()==batch[i]["mperson"]){        	
              	valid = [false, false]; 
              } 
         } 
    	
        
        if ($("#txtcodcontriant").val()==$('#c_codigocontrib').val()){        	
        	valid = [false, false];
        }
    
        if(valid[0]) {
            buscarContribuyente();        
        } else {
            openDialogWarning("Ingrese un valor en los campos de busqueda.", 380, 150);
        }
    });
    $("#c_codigocontrib").focus();
}); 
</script> 